package praca.gra;

import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayList;
import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import praca.framework.FPSCounter;
import praca.framework.GraphicsGL;
import praca.framework.MusicGL;
import praca.framework.TextureGL;
import praca.framework.TouchEventGL;
import praca.framework.Vertices;
import praca.interfejsy.Game;
import praca.interfejsy.Graphics;
import praca.interfejsy.Input;
import praca.interfejsy.Music;
import praca.interfejsy.Screen;
import praca.interfejsy.Texture;
import praca.interfejsy.TouchEvent;
import android.util.Log;

public class StartScreen extends Screen{

	Graphics g;
	Input input;
	List<TouchEvent> lista;
	Vertices vert = new Vertices(100,100, true);
	Texture texture;
	Game game;
	GL10 gl;
	Troll[] trolls;
	Music music;
	List<Texture>  textures = new ArrayList<Texture>();
	float rotation = 0;
	TouchEvent event;
	final int NR_TROLLS = 200;
	FPSCounter counter = new FPSCounter();
	Troll troll;
	FloatBuffer vertices;
	ShortBuffer indices;
	
	public StartScreen(Game game){
		super(game);
		g = game.getGraphics();
		input = game.getInput();
		this.game = game;
		trolls = new Troll[NR_TROLLS];
		for(int i = 0; i < NR_TROLLS; i++) {
			trolls[i] = new Troll(100,100);
		}
		gl = g.getGl();
		texture = new TextureGL(game, "troll.png");
		textures.add(texture);
		music = new MusicGL(game, "trololo.ogg");
		troll = new Troll(100,100);
	}
	

	@Override
	public void update(float deltaTime) {

		g.drawColor(1,1 , 1);

		rotation=rotation + 10*deltaTime;
		if(rotation >= 360)
			rotation=0;
		
		
		if(!input.getTouchEvents().isEmpty()){
				event = input.getTouchEvents().remove(input.getTouchEvents().size()-1);
					for(int j = 0; j < input.getTouchEvents().size(); j++){
						input.getTouchEvents().remove(input.getTouchEvents().size()-1);
					}

				
				if(event.getType() == TouchEventGL.TOUCH_DOWN){
					//Log.d("touch", "Touch down");
					for(int i = NR_TROLLS-1 ; i >= 0; i--){
						if(trolls[i].isInside(event.getX(), event.getY())){
							Log.d("touch", "Dotknales trolla");
							trolls[i].isTouched=true;
							break;
						}else{
							Log.d("touch", "Nie Dotknales trolla");
						}
					}
					
				}
				
				if(event.getType() == TouchEventGL.TOUCH_MOVE){
					//Log.d("touch", "Touch move");
					for(int i = NR_TROLLS-1 ; i >= 0; i--){
						if(trolls[i].isTouched){
							
							trolls[i].x = event.getX();
							trolls[i].y = event.getY();
							break;
						}
					}
				}
				
				if(event.getType() == TouchEventGL.TOUCH_UP){
					//Log.d("touch", "Touch up");
					for(int i = NR_TROLLS-1 ; i >= 0; i--){
						if(trolls[i].isTouched){
							trolls[i].isTouched=false;
							break;
						}
					}
					
				}
				
				
			
		}//koniec if
		
				
		for(int i = 0 ; i < NR_TROLLS; i++){
			trolls[i].update(deltaTime);
				g.clearTransformations();
				g.moveTexture(trolls[i].x, trolls[i].y);
				g.rotateTexture(trolls[i].rotation);
				g.drawTexture(texture, vert);
		}
		
	}



	@Override
	public void pause() {
		music.pause();
		
	}

	@Override
	public void resume() {
		//metoda wywolywana automatycznie podczas pierwszego uruchomienia aplikacji,
		//oraz przy wznawianiu po odblokowaniu telefonu lub przywroceniu aplikacji
		Log.d("touch","Resume w screen");
		music.play();
		texture = new TextureGL(game, "troll.png");
		g.drawColor(1,1 , 1);
		g.setTextureTransparentBackground();
		
	}




	@Override
	public void dispose() {
		music.stop();
		
	}

	

}
